const db = require("../db/index");
// 导入加密中间件
const bcrypt = require("bcryptjs");
const fs = require("fs");

// 产品字段
// product_id,
// product_name,
// product_category,
// product_unit,
// product_inwarehouse_number,
// product_single_price,
// product_create_person,
// in_memo,
// product_all_price
// product_create_time

// 出库字段
// id,
// product_out_id,
// product_out_status,
// product_out_number,
// product_out_price,
// product_out_apply_person,
// audit_memo,
// product_apply_date
// product_out_audit_person,

const productController = {
  // 创建产品
  createProduct: async (req, res) => {
    const {
      product_id,
      product_name,
      product_category,
      product_unit,
      product_inwarehouse_number,
      product_single_price,
      product_create_person,
      in_memo,
    } = req.body;
    // 判断编号是否唯一
    const sql1 = "SELECT * FROM product WHERE product_id = ?";
    const [data] = await db.query(sql1, product_id);
    if (data.length) {
      return res.cc("产品入库编号已存在");
    }
    const product_create_time = new Date();
    const product_all_price = product_single_price * product_inwarehouse_number;
    let product_status = "";
    if (product_inwarehouse_number <= 100) {
      product_status = "库存过少";
    } else if (product_inwarehouse_number >= 300) {
      product_status = "库存过剩";
    } else {
      product_status = "正常";
    }

    const sql = "INSERT INTO product SET ?";
    try {
      await db.query(sql, {
        product_id,
        product_name,
        product_category,
        product_unit,
        product_inwarehouse_number,
        product_single_price,
        product_create_person,
        product_create_time,
        product_all_price,
        product_status,
        in_memo,
      });
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 删除产品
  deleteProductList: async (req, res) => {
    const { id } = req.body;
    const sql = "DELETE FROM product WHERE id = ?";
    try {
      await db.query(sql, id);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 编辑产品信息
  editProduct: async (req, res) => {
    const {
      product_name,
      product_category,
      product_unit,
      product_inwarehouse_number,
      product_single_price,
      in_memo,
      id,
    } = req.body;
    let product_status = "";
    if (product_inwarehouse_number <= 100) {
      product_status = "库存过少";
    } else if (product_inwarehouse_number >= 300) {
      product_status = "库存过剩";
    } else {
      product_status = "正常";
    }
    const product_update_time = new Date();
    const product_all_price = product_single_price * product_inwarehouse_number;
    const sql =
      "UPDATE product SET product_name = ?,product_category = ?,product_unit = ?,product_inwarehouse_number = ?,product_single_price = ?,product_all_price = ?,product_update_time = ?,in_memo = ?, product_status = ? WHERE id = ?";
    try {
      await db.query(sql, [
        product_name,
        product_category,
        product_unit,
        product_inwarehouse_number,
        product_single_price,
        product_all_price,
        product_update_time,
        in_memo,
        product_status,
        id,
      ]);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取产品列表
  getProductList: async (req, res) => {
    const { page } = req.query;
    const sql = `SELECT * FROM product WHERE product_inwarehouse_number >= 0 ORDER BY product_create_time DESC  LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = "SELECT * FROM product WHERE product_inwarehouse_number >= 0";
    try {
      const [data] = await db.query(sql);
      const [total] = await db.query(sql1);
      res.send({
        status: 1,
        message: "成功",
        data: data,
        total: total.length,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  //产品申请出库
  applyOutProduct: async (req, res) => {
    const {
      id,
      product_out_id,
      product_out_number,
      product_single_price,
      product_out_apply_person,
      apply_memo,
    } = req.body;
    const product_apply_time = new Date();
    const product_out_price = product_single_price * product_out_number;
    // 判断编号是否唯一
    const sql1 =
      "SELECT * FROM product WHERE product_out_id = ? AND product_out_status = '申请出库'";
    const product_out_status = "申请出库";
    const [data] = await db.query(sql1, product_out_id);
    if (data.length) {
      return res.cc("产品出库编号已存在");
    }
    const sql =
      "UPDATE product SET audit_memo = NULL, product_out_status = ?,product_out_id = ?,product_out_number = ?,product_single_price = ?,product_out_price = ?,product_out_apply_person = ?,apply_memo = ?,product_apply_time = ? WHERE id = ?";
    try {
      await db.query(sql, [
        product_out_status,
        product_out_id,
        product_out_number,
        product_single_price,
        product_out_price,
        product_out_apply_person,
        apply_memo,
        product_apply_time,
        id,
      ]);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 产品审核列表
  applyProductList: async (req, res) => {
    const { page } = req.query;
    const sql = `SELECT * FROM product WHERE product_out_status not in ('同意')  ORDER BY product_create_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 =
      "SELECT * FROM product WHERE product_out_status not in ('同意')";
    try {
      const [data] = await db.query(sql);
      const [total] = await db.query(sql1);
      res.send({
        status: 1,
        message: "成功",
        data: data,
        total: total.length,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 撤回申请
  withdrawApplyProduct: async (req, res) => {
    const sql =
      "UPDATE product SET product_out_id = NULL,product_out_number = NULL, product_out_status = NULL,product_out_apply_person = NULL,audit_memo = NULL,product_apply_time = NULL WHERE id = ?";
    try {
      await db.query(sql, req.body.id);
      res.send({
        status: 1,
        message: "成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 产品审核
  auditProduct: async (req, res) => {
    const {
      id,
      product_out_id,
      product_out_status,
      audit_memo,
      product_out_audit_person,
      product_out_apply_person,
      product_inwarehouse_number,
      product_single_price,
      product_out_number,
    } = req.body;
    // 审核时间
    const product_audit_time = new Date();

    try {
      if (product_out_status === "同意") {
        // 更新库存
        const newWarehouseNumber =
          product_inwarehouse_number - product_out_number;
        // 更新产品总价
        const product_all_price = newWarehouseNumber * product_single_price;
        // 更新库存状态
        let product_status = "";
        if (product_inwarehouse_number <= 100) {
          product_status = "库存过少";
        } else if (product_inwarehouse_number >= 300) {
          product_status = "库存过剩";
        } else {
          product_status = "正常";
        }
        const sql = "INSERT INTO outproduct SET ?  ";
        await db.query(sql, {
          product_out_id,
          product_out_number,
          product_out_price: product_out_number * product_single_price,
          product_out_audit_person,
          product_out_apply_person,
          product_audit_time,
          audit_memo,
        });

        // 仓库信息清空
        const sql1 =
          "UPDATE product SET product_out_id = NULL,product_out_status = NULL,product_status = ?,product_all_price = ?,product_inwarehouse_number = ?, product_out_number = NULL,product_out_apply_person = NULL,product_out_price = NULL,product_apply_time = NULL WHERE id = ?";
        await db.query(sql1, [
          product_status,
          product_all_price,
          newWarehouseNumber,
          id,
        ]);
        res.send({
          status: 1,
          message: "成功",
        });
      } else if (product_out_status === "否决") {
        const sql =
          "UPDATE product SET audit_memo = ?, product_out_status = '否决',product_audit_time = ?,product_out_audit_person = ? WHERE id = ?";
        await db.query(sql, [
          audit_memo,
          product_audit_time,
          product_out_audit_person,
          id,
        ]);
        res.send({
          status: 1,
          message: "成功",
        });
      }
    } catch (err) {
      res.cc(err);
    }
  },
  // 出库列表
  getAuditProductList: async (req, res) => {
    const { page } = req.query;
    const sql = `SELECT * FROM outproduct  ORDER BY product_audit_time DESC LIMIT 10 OFFSET ${
      (page - 1) * 10
    }`;
    const sql1 = "SELECT * FROM outproduct ";
    try {
      const [data] = await db.query(sql);
      const [total] = await db.query(sql1);
      res.send({
        status: 1,
        message: "成功",
        data: data,
        total: total.length,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 通过入库编号搜索
  searchProductForId: async (req, res) => {
    const { product_id } = req.query;
    console.log(product_id);

    const sql =
      "SELECT * FROM product WHERE product_id = ?  ORDER BY product_create_time DESC";
    const [data] = await db.query(sql, product_id);
    res.send({
      status: 1,
      message: "成功",
      data: data,
    });
  },
  // 通过出库申请编号搜索
  searchProductForApplyId: async (req, res) => {
    const { product_out_id } = req.query;
    const sql =
      "SELECT * FROM product WHERE product_out_id = ?  ORDER BY product_create_time DESC";
    const [data] = await db.query(sql, product_out_id);
    res.send({
      status: 1,
      message: "成功",
      data: data,
    });
  },
  // 通过出库编号搜索
  searchProductForOutId: async (req, res) => {
    const { product_out_id } = req.query;
    const sql =
      "SELECT * FROM outProduct WHERE product_out_id = ?  ORDER BY product_create_time DESC";
    const [data] = await db.query(sql, product_out_id);
    res.send({
      status: 1,
      message: "成功",
      data: data,
    });
  },
};

module.exports = productController;
